<?php namespace Admin\Controller;
	
	use Common\Model\Glist;
//	类型属性页面控制器
	class GoodsListController extends CommonController{
		
		private $db;
		public function __init(){
			parent::__init();
			$this->db=new Glist;
		}
		
//		货品列表页面
		public function index(){
		    $gid=Q('get.gid',0);
		    $tid=Q('get.tid',0,'intval' );

				$listData=$this->db->where('gid',$gid)->get();
//				p($listData[0]['combine']);die;
			
				
				foreach($listData as $k=>$v){
					
				$listData[$k]['combine']=explode(',', $v['combine']);
				}
				$atModel=new \Common\Model\goods_attr;
				foreach($listData as $k=>$v){
				foreach($listData[$k]['combine'] as $kk=>$vv){
				//				var_dump($vv);
				$listData[$k]['combine'][$kk]=$atModel->where('gtid',$vv)->pluck('gtvalue');
				}
				}
//						p($listData);die;
				View::with('listData', $listData);

				
              $data=Db::table('goods_attr ga')
              ->join('type_attr ta','ga.taid','=','ta.taid')
              ->where('gid',$gid)
              ->where('class',1)
              ->get();
//			p($data[0]['goods_gid']);die;
//			
            $data2=$this->db->where('gid',$gid)->get();
//			p($data2);die;

			
            View::with('gid',$gid);
            View::with('data',$data);
			View::make();
		}
		
		
		
//		ajax方法
		public function check(){
		$gid=Q('get.gid',0,'intval');
		
		$combine=Q('get.combine',0,'intval');
//		p($combine);die;
		$listModel=new \Common\Model\goods_attr;
		echo $listModel->where('gid',$gid)->where('combine',implode(',', $combine))->count()? 0 : 1;
		
	}
		
		
		
		
		
		public function add(){
	 $gid=Q('post.gid',0,'intval');
//	var_dump($gid);die;	
	  if(IS_POST){
//			p($_POST);die;
       $result=$this->db->store();
	   	if(!$result){
				View::error($this->db->getError());
			}
		View::success('添加成功',U('index',array('gid'=>$gid)));
	} 
		
	}
//		删除货品数据
		public function del(){
			$glid=Q('get.glid',0,'intval');
	        $gid=Q('get.gid',0,'intval');
	        $this->db->where('glid',$glid)->delete();
		    View::success('删除成功',U('index',array('gid'=>$gid)));
		}
		
		public function edit(){
			$gid=Q('get.gid',0);
		    $glid=Q('get.glid',0,'intval' );
                
				if(IS_POST){
//			p($_POST);die;
                    $result=$this->db->edit();
	            	if(!$result){
			            View::error($this->db->getError());
	             	}
		            View::success('编辑成功',U('index',array('gid'=>$gid)));
	            }  
				
				
				$listData=$this->db->where('gid',$gid)->get();
//				p($listData);die;
				foreach($listData as $k=>$v){
				$listData[$k]['combine']=explode(',', $v['combine']);
				}
				$atModel=new \Common\Model\goods_attr;
				foreach($listData as $k=>$v){
				foreach($listData[$k]['combine'] as $kk=>$vv){
				//				var_dump($vv);
				$listData[$k]['combine'][$kk]=$atModel->where('gtid',$vv)->pluck('gtvalue');
				}
				}
//						p($listData);die;
				View::with('listData', $listData);

				$data=Db::table('goods_attr ga')->join('type_attr ta','ga.taid','=','ta.taid')->where('gid',$gid)->where('class',1)->get();
//			p($data);die;
//			
            
			$oldData=$this->db->where('glid',$glid)->find();
			$oldData['combine']=explode(',', $oldData['combine']);
			View::with('oldData', $oldData);

			View::with('gid', $gid);
            View::with('glid', $glid);

            View::with('data',$data);
			
			View::make();
		}
		
	}
	
	
	
	
	
	
	
	
	
	?>